<template>
  <div class="card">
    <div class="card">
      <el-input v-model="data.title" placeholder="请输入想要搜索的标题名称" style="width: 300px; margin-right: 10px"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
      <el-button type="success" @click="handleAdd">新增公告</el-button>
      <el-button type="danger" @click="delBatch">批量删除</el-button>
    </div>
    <div class="card" style="margin-top: 5px">
      <el-table :data="data.tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column label="公告标题" prop="title"></el-table-column>
        <el-table-column label="公告内容" prop="content"></el-table-column>
        <el-table-column label="发布时间" sortable prop="time"></el-table-column>
        <el-table-column label="操作" min-width="160">
          <template #default="scope">
            <el-button type="primary" round plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" round plain @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="card" style="margin-top: 5px;">
      <el-pagination background layout="->, total, prev, pager, next" @current-change="load"
                     v-model:page-size="data.pageSize" v-model:current-page="data.pageNum"
                     :total="data.total"></el-pagination>
    </div>
    <!--弹窗页面-->
    <el-dialog v-model="data.formVisible" title="系统公告" width="40%">
      <el-form :model="data.form" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="data.form.title"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="data.form.content" type="textarea"/>
        </el-form-item>
        <el-form-item>
          <el-button @click="data.formVisible=false">取 消</el-button>
          <el-button type="primary" @click="save">保 存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import {reactive} from "vue";
import request from "@/utils/request";
import {ElMessage, ElMessageBox } from "element-plus";

const data = reactive({
  form: {
    tableData: [],
    total: 0,
    pageNum: 1,
    pageSize: 10,
    title: '',
    content: '',
    form: {},
    formVisible: false,
    ids: [],
  }
})

/**
 * 分页查询
 */
const load = () => {
  request.get('/notice/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      title: data.title
    }
  }).then(res => {
    data.tableData = res.data.list
    data.total = res.data.total
  })
}
load()

/**
 * 弹出新增窗口
 */
const handleAdd = () => {
  data.form = {}
  data.formVisible = true
}

/**
 * 新增
 */
const add = () => {
  //输入为空，显示错误信息
  if((!data.form.title) || (!data.form.content)){
    ElMessage.error('输入内容不能为空')
  }else {
      request.post('/notice/add', data.form).then(res => {
        if (res.code === '200') {
          load()
          data.formVisible = false
          ElMessage.success('操作成功')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
}

/**
 * 弹出编辑窗口
 */
const handleEdit = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}

/**
 * 编辑
 */
const edit = () => {
  if(!data.form.title || !data.form.content){
    ElMessage.error('更新后内容不能为空')
    //更新前后标题和内容不变动时不请求后台
  }else {
    request.put('/notice/update', data.form).then(res => {
      if (res.code === '200') {
        load()
        data.formVisible = false
        ElMessage.success('操作成功')
      } else {
        ElMessage.error(res.msg)
      }
    })
  }
}

/**
 * 新增 or 编辑
 */
const save = () => {
  data.form.id ? edit() : add()
}

/**
 * 删除
 * @param id
 */
const del = (id) => {
  ElMessageBox.confirm('删除后无法恢复，确定要删除吗？', '确认删除', {type: "warning"}).then(res => {
    request.delete('/notice/delete/' + id).then(res => {
      if (res.code === '200') {
        load()
        ElMessage.success('操作成功')
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(err => {
  })
}

/**
 * 批量删除
 * @param rows
 */
const handleSelectionChange = (rows) => {
  data.ids = rows.map(row => row.id)
}
const delBatch = () => {
  if(!data.ids){
    ElMessage.error('请选择要删除的数据')
  }else{
    ElMessageBox.confirm('删除后无法恢复，确定要删除吗？', '确认删除', {type: "warning"}).then(res => {
      request.delete('/notice/delete-batch' , {data:data.ids}).then(res => {
        if (res.code === '200') {
          load()
          ElMessage.success('操作成功')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }).catch(err => {
    })
  }
}

const reset = () => {
  data.title = null
  load()
}
</script>

